package com.ffcs.cn.action.base.dialog;

import com.ffcs.cn.DevelopProperties;
import com.supermap.data.*;
import com.supermap.desktop.controls.ui.UICommonToolkit;
import com.supermap.desktop.controls.ui.controls.DialogResult;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.button.SmButton;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDataset;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDatasource;
import com.supermap.desktop.controls.utilities.ComponentFactory;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IFormTabular;
import com.supermap.desktop.core.implement.SmStatusbar;
import com.supermap.desktop.core.properties.CoreProperties;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.CursorUtilities;
import com.supermap.desktop.core.utilties.FormUtilities;
import com.supermap.desktop.tabularview.FormTabular;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
  * 打开自定义属性表
 */
public class DialogOpenTabular extends SmDialog {
	private JLabel labelDatasource;
	private SmComboBoxDatasource datasourceComboBox;
	private JLabel labelDataset;
	private SmComboBoxDataset datasetComboBox;
	private JCheckBox checkBoxShowInDialog;
	private SmButton buttonOK;
	private SmButton buttonCancel;

	private IFormTabular formTabular = null;

	public DialogOpenTabular() {
		initComponent();
		initLayout();
		initListeners();
	}

	private void initComponent() {
		setTitle(DevelopProperties.getString("String_OpenTabular"));
		labelDatasource = new JLabel(DevelopProperties.getString("String_Label_DatasourceName"));
		datasourceComboBox = new SmComboBoxDatasource();
		labelDataset = new JLabel(DevelopProperties.getString("String_Label_DatasetName"));
		datasetComboBox = new SmComboBoxDataset();
		checkBoxShowInDialog = new JCheckBox();
		checkBoxShowInDialog.setText("弹窗显示属性表");
		buttonOK = ComponentFactory.createButtonOK();
		buttonCancel = ComponentFactory.createButtonCancel();
		datasetComboBox.setDatasource(datasourceComboBox.getSelectedDatasource());
		setSize(new Dimension(300,160));
	}

	public void initLayout() {
		setLayout(new GridBagLayout());
		add(labelDatasource, new GridBagConstraintsHelper(0,0,1,1)
				.setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setWeight(0,0)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0));
		add(datasourceComboBox, new GridBagConstraintsHelper(1,0,1,1)
				.setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.HORIZONTAL).setWeight(1,0)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP));

		add(labelDataset, new GridBagConstraintsHelper(0,1,1,1)
				.setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setWeight(0,0)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0));
		add(datasetComboBox, new GridBagConstraintsHelper(1,1,1,1)
				.setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.HORIZONTAL).setWeight(1,0)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP));

		add(checkBoxShowInDialog, new GridBagConstraintsHelper(0,2,2,1)
				.setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setWeight(1,0)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0));

		add(ComponentFactory.createButtonPanel(buttonOK, buttonCancel), new GridBagConstraintsHelper(0,99,2,1)
				.setAnchor(GridBagConstraints.SOUTHEAST).setFill(GridBagConstraints.NONE).setWeight(1,1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP));
	}

	private void initListeners() {
		buttonOK.addActionListener(e -> {
			CursorUtilities.setWaitCursor();
			try {
				try {
					Dataset selectedDataset = datasetComboBox.getSelectedDataset();
					Recordset recordset = null;
					DatasetVector datasetVector = null;
					if (selectedDataset instanceof DatasetVector) {
						datasetVector = (DatasetVector)selectedDataset;
					} else if (selectedDataset instanceof DatasetMosaic) {
						datasetVector = ((DatasetMosaic) selectedDataset).getFootprintDataset();
					}else {
						UICommonToolkit.showMessageDialog("需要选择一个矢量数据集。");
						return;
					}
					// 从数据集拿到记录集
					recordset = datasetVector.getRecordset(false, CursorType.DYNAMIC);

					// 根据记录集打开属性表
					IFormTabular formTabular = openFormTabular(recordset);
					DialogOpenTabular.this.formTabular = formTabular;

					if (formTabular == null) {
						Application.getActiveApplication().getOutput().output(CoreProperties.getString("String_OpenDatasetVectorFormTabular_Failed"));
					} else {
						DialogOpenTabular.this.dispose();
					}
				} catch (Exception ex) {
					Application.getActiveApplication().getOutput().output(ex);
				}
			} catch (Exception ex) {
				Application.getActiveApplication().getOutput().output(ex);
			} finally {
				setDialogResult(DialogResult.OK);
				CursorUtilities.setDefaultCursor();
			}
		});
		buttonCancel.addActionListener(e -> DialogOpenTabular.this.dispose());
	}

	private  @Nullable IFormTabular openFormTabular(Recordset recordset) {
		FormTabular formTabular = null;
		if (checkBoxShowInDialog.isSelected()) {
			// 弹窗显示属性表，直接新建属性表面板。
			// 这个FormTabular就是一个普通的panel，可以嵌套在弹窗/dockbar里面
			formTabular = new FormTabular("我的属性表窗口");
			// 给属性表设置recordset
			formTabular.setRecordset(recordset);
			SmDialog dialog=  new SmDialog(this);
			Container contentPane = dialog.getContentPane();
			contentPane.setLayout(new GridBagLayout());

			contentPane.add(formTabular, new GridBagConstraintsHelper(0, 0, 1, 1).setWeight(1, 1).setFill(GridBagConstraints.BOTH));
			dialog.setSize(new Dimension(500,500));
			// 下方的状态栏
			SmStatusbar statusbar = formTabular.getStatusbar();
			// 可以通过这个方法拿到对应的控件来进行显示，具体ID可以从Tabular.config中查看
			statusbar.getComponent("XXX");

			dialog.showDialog();
		}else {
			// 直接公共类发送创建事件，添加到子窗口中
			formTabular = (FormTabular) FormUtilities.fireNewTabularWindowEvent(recordset);
		}
		return formTabular;
	}

	/**
	 *  设置 checkBoxShowInDialog 是否可见
	 * @param visible 是否可见
	 */
	public void setCheckBoxShowInDialogVisible(boolean visible) {
		this.checkBoxShowInDialog.setVisible(visible);
	}

	public IFormTabular getFormTabular(){
		return formTabular;
	}
}
